home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / PMUPDT13.ZIP / VME.ZIP / RAND.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-04-15  |  2.4 KB  |  80 lines

  1. ;Linear Congruential Pseudo-Random Number Generator
  2.  
  3.                 .model  small
  4.  
  5.                 .code
  6.  
  7.                 public  RANDOM_SEED
  8.                 public  GET_RANDOM
  9.  
  10.  
  11. ;The generator is defined by the equation
  12. ;
  13. ;              X(N+1) = (A*X(N) + C) mod M
  14. ;
  15. ;where the constants are defined as
  16. ;
  17. M               EQU     43691           ;large prime
  18. A               EQU     M+1
  19. C               EQU     14449           ;large prime
  20. RAND_SEED       DW      0               ;X0, initialized by RANDOM_SEED
  21.  
  22. ;Set RAND_SEED up with a random number to seed the pseudo-random number
  23. ;generator. This routine should preserve all registers! it must be totally
  24. ;relocatable!
  25. RANDOM_SEED     PROC    NEAR
  26.                 push    si
  27.                 push    ds
  28.                 push    dx
  29.                 push    cx
  30.                 push    bx
  31.                 push    ax
  32.                 call    RS1
  33. RS1:            pop     bx
  34.                 sub     bx,OFFSET RS1
  35.                 xor     ax,ax
  36.                 mov     ds,ax
  37.                 mov     si,46CH
  38.                 lodsw
  39.                 xor     dx,dx
  40.                 mov     cx,M
  41.                 div     cx
  42.                 mov     WORD PTR cs:[bx][RAND_SEED],dx
  43.                 pop     ax
  44.                 pop     bx
  45.                 pop     cx
  46.                 pop     dx
  47.                 pop     ds
  48.                 pop     si
  49.                 retn
  50.  
  51. RANDOM_SEED     ENDP
  52.  
  53.  
  54. ;Create a pseudo-random number and put it in ax. This routine must preserve
  55. ;all registers except ax!
  56. GET_RANDOM      PROC    NEAR
  57.                 push    bx
  58.                 push    cx
  59.                 push    dx
  60.                 call    GR1
  61. GR1:            pop     bx
  62.                 sub     bx,OFFSET GR1
  63.                 mov     ax,WORD PTR cs:[bx][RAND_SEED]
  64.                 mov     cx,A                            ;multiply
  65.                 mul     cx
  66.                 add     ax,C                            ;add
  67.                 adc     dx,0
  68.                 mov     cx,M
  69.                 div     cx                              ;divide
  70.                 mov     ax,dx                           ;remainder in ax
  71.                 mov     cs:WORD PTR [bx][RAND_SEED],ax  ;and save for next round
  72.                 pop     dx
  73.                 pop     cx
  74.                 pop     bx
  75.                 retn
  76.  
  77. GET_RANDOM      ENDP
  78.  
  79.                 END
  80.